package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//对牌进行一系列操作
public class CardGame {
    //红桃 梅花 方片 黑桃
    public static final String[] suits = {"♥","♣","♦","♠"};

    /*
    生成一副扑克牌
    52张  不包含大小王
     */
    public List<Card> buyCard(){//买牌
        //创建一个一位数组存储牌
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                //实例化一张扑克牌  传入参数 并调用他的构造方法
                Card card = new Card(suits[i],j);
                //将实例化的每一张牌放到该一维数组中
                cardList.add(card);
            }
        }
        return cardList;
    }

    //洗牌
    public void shuffle(List<Card> cardList){
        Random random = new Random();
        for (int i = cardList.size() - 1; i > 0; i--) {
            int index = random.nextInt(i);
            swap(cardList,i,index);
        }
    }
    //交换牌
    public static void swap(List<Card> cardList,int i,int j){
        //get 获取指定索引处的元素
        Card temp = cardList.get(i);
        //public E set(int index,E element):
        //将指定元素替换index位置处的元素并返回此位置之前的元素
        cardList.set(i,cardList.get(j));
        cardList.set(j,temp);
    }

    //发牌
    public List<List<Card>> getCard(List<Card> cardList){
        //定义三个用户 相当于三个一维数组存放各自的牌
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(j);
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
